#include "gfxTool.h"

gfxTool::gfxTool(float radius, float neighborsLength, float growthRate, ToolMode primaryMode, ToolMode secondaryMode)
{
	Position = new Vertex3f(1, 1, 1);
	SelectedVertex = 0;
	Radius = radius;
	NeighborsLength = neighborsLength;
	GrowthRate = growthRate;
	Area = 0;
	Distances = 0;
	CenterIndices = CenterParents = 0;

	PrimaryMode = primaryMode;
	SecondaryMode = secondaryMode;
	HasSelection = false;
}

void gfxTool::SetPosition(float x, float y, float z)
{
	this->Position->X = x;
	this->Position->Y = y;
	this->Position->Z = z;
}

void gfxTool::SetPosition(Vertex3f *newPosition)
{
	this->Position->X = newPosition->X;
	this->Position->Y = newPosition->Y;
	this->Position->Z = newPosition->Z;
}

void gfxTool::SwitchModes()
{
	ToolMode temp = PrimaryMode;
	PrimaryMode = SecondaryMode;
	SecondaryMode = temp;
}

void gfxTool::ColorSelectedArea()
{
	bool sign = true;
	float distance;
	float halfNeighborsLength = NeighborsLength / 2.0f;
	Vertex3f *vertex;
	for (int i = 0; i < Area->count() ; i++)
	{
		vertex = Area->at(i);
		vertex->IsColored = true;
		distance = Distances->at(i);
		if(distance == ZeroDistance)
		{
			vertex->SetColor(255, 0, 0, 255);	//red
		}
		else
		{
			if(distance < halfNeighborsLength)
			{
				vertex->SetColor(255, (distance * 510 / NeighborsLength), 0, 255);
			}
			else if(distance <= NeighborsLength)
			{
				vertex->SetColor(0, 510 - (distance * 510 / NeighborsLength), 255, 255);
			}
		}
	}
}


void gfxTool::FreeSelectedAreaInformation()
{
	if(Area != 0)
	{
		Vertex3f *vertex;
		for (int i = 0; i < Area->count(); i++)
		{
			vertex = Area->at(i);
			vertex->IsColored = false;
		}
		delete Area;
		delete Distances;
		Area = 0;
		Distances = 0;

		if(CenterIndices != 0)
		{
			HasSelection = false;
			delete CenterIndices;
			delete CenterParents;
			CenterIndices = CenterParents = 0;
		}
	}
}

gfxTool::~gfxTool(void)
{
	delete Position;
}
